from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QTextEdit, QComboBox
from PyQt5.QtGui import QFont
from PyQt5.QtCore import Qt

class Terminal(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setup_ui()
    
    def setup_ui(self):
        """设置终端界面"""
        # 主布局
        main_layout = QVBoxLayout(self)
        main_layout.setContentsMargins(0, 0, 0, 0)
        main_layout.setSpacing(0)
        
        # 终端标题栏
        title_bar = QWidget()
        title_bar.setFixedHeight(30)
        title_layout = QHBoxLayout(title_bar)
        title_layout.setContentsMargins(10, 0, 10, 0)
        
        # 终端标题
        title_label = QLabel("终端")
        title_font = QFont()
        title_font.setBold(True)
        title_label.setFont(title_font)
        title_layout.addWidget(title_label)
        
        title_layout.addStretch()
        
        # 终端类型选择
        self.terminal_type = QComboBox()
        self.terminal_type.addItems(["CMD", "PowerShell", "Python", "Git Bash"])
        self.terminal_type.setFixedWidth(120)
        title_layout.addWidget(self.terminal_type)
        
        # 控制按钮
        self.clear_btn = QPushButton("清空")
        self.clear_btn.setFixedSize(60, 24)
        self.clear_btn.clicked.connect(self.clear_terminal)
        title_layout.addWidget(self.clear_btn)
        
        main_layout.addWidget(title_bar)
        
        # 终端输出区域
        self.output_area = QTextEdit()
        self.output_area.setReadOnly(True)
        
        # 设置终端字体和样式
        # 使用支持中文的字体，优先选择系统字体
        terminal_font = QFont()
        terminal_font.setFamily("Microsoft YaHei, SimHei, Consolas, Monospace")
        terminal_font.setPointSize(10)
        terminal_font.setStyleHint(QFont.Monospace)
        self.output_area.setFont(terminal_font)
        
        main_layout.addWidget(self.output_area)
        
        # 设置样式
        self.setStyleSheet("""
            QWidget {
                background-color: #1e1e1e;
                color: #cccccc;
            }
            
            QLabel {
                color: #cccccc;
                background-color: transparent;
            }
            
            QTextEdit {
                background-color: #0c0c0c;
                color: #00ff00;
                border: none;
                font-family: "Microsoft YaHei", "SimHei", "Consolas", monospace;
                font-size: 10pt;
            }
            
            QComboBox {
                background-color: #2d2d2d;
                color: #cccccc;
                border: 1px solid #444;
                border-radius: 3px;
                padding: 2px 8px;
            }
            
            QComboBox::drop-down {
                border: none;
            }
            
            QComboBox::down-arrow {
                image: none;
                border-left: 4px solid transparent;
                border-right: 4px solid transparent;
                border-top: 4px solid #cccccc;
            }
            
            QPushButton {
                background-color: #2d2d2d;
                color: #cccccc;
                border: 1px solid #444;
                border-radius: 3px;
                padding: 4px 8px;
            }
            
            QPushButton:hover {
                background-color: #383838;
            }
            
            QPushButton:pressed {
                background-color: #1e1e1e;
            }
        """)
        
        # 显示欢迎信息
        self.show_welcome_message()
    
    def show_welcome_message(self):
        pass
    
    def clear_terminal(self):
        """清空终端输出"""
        self.output_area.clear()
        self.show_welcome_message()
    
    def append_output(self, text):
        """添加输出到终端"""
        current_text = self.output_area.toPlainText()
        self.output_area.setPlainText(current_text + text + "\n")
        
        # 滚动到底部
        cursor = self.output_area.textCursor()
        cursor.movePosition(cursor.End)
        self.output_area.setTextCursor(cursor)
    
    def execute_command(self, command):
        """执行命令（模拟）"""
        self.append_output(f"> {command}")
        
        # 模拟命令执行结果
        if command == "help":
            self.append_output("""
可用命令:
- help: 显示此帮助信息
- clear: 清空终端
- python: 启动Python解释器
- git: Git版本控制命令
- exit: 退出终端
""")
        elif command == "clear":
            self.clear_terminal()
        elif command.startswith("python"):
            self.append_output("Python 3.9.0 (default, Oct 6 2021, 03:47:53)")
            self.append_output("Type 'help', 'copyright', 'credits' or 'license' for more information.")
        else:
            self.append_output(f"命令 '{command}' 未找到或未实现")
    
    def get_terminal_type(self):
        """获取当前终端类型"""
        return self.terminal_type.currentText()